////////////////////////
// Constants &	      //
// globals	      //
////////////////////////

plugin.onShowOptionsDlg = OnInitOptions;

var T_SKIN_H = 15;
var T_SKIN_W = 200;
var A_LIST_H = 80;
var BUTTON_H = 22;
var BUTTON_W = 90;
var PART2 = T_SKIN_H*7+T_SKIN_H+A_LIST_H;
var PART3 = PART2+T_SKIN_H*4;
var MARGIN_LEFT = 5;

var _wndOptions;
var _lastSelectedClock = 0;

/////////////////////
// Event	   //
// handlers	   //
/////////////////////

function OnInitOptions(wnd) {
  _wndOptions = wnd;
  _lastSelectedClock = 0;
  // select skin (list)
  wnd.AddControl(gddWndCtrlClassLabel, 0,
		 "", SKINS , MARGIN_LEFT, 0, T_SKIN_W, T_SKIN_H);
  var values = SKINS_LIST;
  wnd.AddControl(gddWndCtrlClassList, gddWndCtrlTypeListDrop,
		 "s_list", values, MARGIN_LEFT, T_SKIN_H,
		 T_SKIN_W, T_SKIN_H);
  // select sound
  wnd.AddControl(gddWndCtrlClassLabel, 0,
		 "", SOUNDS , MARGIN_LEFT, T_SKIN_H*3,
		 T_SKIN_W, T_SKIN_H);
  var values = SOUNDS_LIST;
  wnd.AddControl(gddWndCtrlClassList, gddWndCtrlTypeListDrop,
		 "snd_list", values, MARGIN_LEFT, T_SKIN_H*4,
		 T_SKIN_W, T_SKIN_H);

  // timer/alarm list
  wnd.AddControl(gddWndCtrlClassLabel, 0,
		 "t_title", ALARMS , MARGIN_LEFT, T_SKIN_H*6,
		 T_SKIN_W, T_SKIN_H);
  var values = GetClockList();
  wnd.AddControl(gddWndCtrlClassList, gddWndCtrlTypeListOpen,
		 "t_list", values, MARGIN_LEFT, T_SKIN_H*7,
		 T_SKIN_W, A_LIST_H);
  wnd.GetControl("t_list").onChanged = OnList;
  // add timer (button)
  wnd.AddControl(gddWndCtrlClassButton, gddWndCtrlTypeButtonPush,
		 "b_timerd", B_ADD_T , MARGIN_LEFT, PART2, BUTTON_W, BUTTON_H);
  wnd.GetControl("b_timerd").onClicked = OnAddTimer;

  // add alarm (button)
  wnd.AddControl(gddWndCtrlClassButton, gddWndCtrlTypeButtonPush,
		 "b_alarm", B_ADD_A , 100, PART2, BUTTON_W, BUTTON_H);
  wnd.GetControl("b_alarm").onClicked = OnAddAlarm;

  // Add stopwatch
  wnd.AddControl(gddWndCtrlClassButton, gddWndCtrlTypeButtonPush,
		 "b_stopwatch", B_STOPWATCH , MARGIN_LEFT, PART2+T_SKIN_H*2, BUTTON_W, BUTTON_H);
  wnd.GetControl("b_stopwatch").onClicked = OnAddStopwatch;

  // delete
  wnd.AddControl(gddWndCtrlClassButton, gddWndCtrlTypeButtonPush,
		 "b_delete", B_DELETE , 100, PART2+T_SKIN_H*2, BUTTON_W, BUTTON_H);
  wnd.GetControl("b_delete").onClicked = OnDelete;

  ///////////////////////
  // Alarm info	     //
  ///////////////////////

  // Name
  wnd.AddControl(gddWndCtrlClassLabel, 0,
		 "", A_NAME , MARGIN_LEFT, PART3, T_SKIN_W, T_SKIN_H);
  wnd.AddControl(gddWndCtrlClassEdit, 0,
		 "a_name", "" , 58, PART3, BUTTON_W*1.5, BUTTON_H-2);
  //  wnd.GetControl("b_delete").onChanged = OnName;

  // Alarm
  wnd.AddControl(gddWndCtrlClassLabel, 0,
		 "", A_END , MARGIN_LEFT, PART3+T_SKIN_H*2, T_SKIN_W, T_SKIN_H);
  wnd.AddControl(gddWndCtrlClassEdit, 0,
		 "a_time", "hh:mm:ss" , 58, PART3+T_SKIN_H*2, BUTTON_W, BUTTON_H);

  // Duration
  wnd.AddControl(gddWndCtrlClassLabel, 0,
		 "", A_DURATION , MARGIN_LEFT, PART3+T_SKIN_H*4, T_SKIN_W, T_SKIN_H);
  wnd.AddControl(gddWndCtrlClassEdit, 0,
		 "t_duration", "hh:mm:ss" , 58, PART3+T_SKIN_H*4, BUTTON_W, BUTTON_H-2);

  // isActive
  wnd.AddControl(gddWndCtrlClassLabel, 0,
		 "", ACTIVE , MARGIN_LEFT, PART3+T_SKIN_H*6, T_SKIN_W, T_SKIN_H);
  wnd.AddControl(gddWndCtrlClassButton, gddWndCtrlTypeButtonCheck,
		 "active", "" , 58, PART3+T_SKIN_H*6, BUTTON_W, BUTTON_H-2);

  // Command
  wnd.AddControl(gddWndCtrlClassLabel, 0,
		 "", COMMAND , MARGIN_LEFT, PART3+T_SKIN_H*8, T_SKIN_W, T_SKIN_H);
  wnd.AddControl(gddWndCtrlClassEdit, 0,
		 "a_command", "" , 58, PART3+T_SKIN_H*8, BUTTON_W*1.5, BUTTON_H-2);


  /////////////////////////
  // Other stuffs	       //
  /////////////////////////
  wnd.onClose = OnDestroyOptions;
  SetUIClock(-1);
  if (_clocks.length > 0)
    SetUIClock(_clocks[0]);
  wnd.GetControl("s_list").value = options("skinDir");
  wnd.GetControl("snd_list").value = options("sound");
}


/////////////////////
// Event 	   //
// handling	   //
/////////////////////

function OnName(wnd, control) {
  var clockId = GetClockId(wnd.GetControl("t_list").value);
  if (clockId < 0)
    return;
  _clocks[clockId].SetName(control.value);
}

function OnAddAlarm(wnd, control) {
  AddClock(0);
}

function OnAddTimer(wnd, control) {
  AddClock(1);
}

function OnAddStopwatch(wnd, control) {
  AddClock(2);
}

// Deprecated
function  OnModify(wnd, control) {
  Trace("OnModify");
  var clockId = GetClockId(wnd.GetControl("t_list").value);
  SaveClock(clockId);
  wnd.GetControl("t_list").text = GetClockList();
  wnd.GetControl("t_list").value = _clocks[clockId].ListName();
}

function  OnDelete(wnd, control) {
  if (_clocks.length == 0)
    return;
  // get the selected item
  var clockId = GetClockId(wnd.GetControl("t_list").value);
  _currentClock = _clocks.length - 1;
  _lastSelectedClock = 0;
  // remove item from _clocks
  _clocks.splice(clockId, 1);
  DrawDetails();
  wnd.GetControl("t_list").text = GetClockList();
  if (_clocks.length > 0) {
    wnd.GetControl("t_list").value = _clocks[0].ListName();
    SetUIClock(_clocks[0]);
  }
}

function  OnList(wnd, control) {
  if (_clocks.length == 0)
    return;
  var clockId = GetClockId(control.value);
  SaveClock(_lastSelectedClock);
  // get the selected item
  _lastSelectedClock = clockId;
  // Load clock details
  SetUIClock(_clocks[clockId]);
  wnd.GetControl("t_list").text = GetClockList();
  Trace(_clocks[clockId].ListName());
  wnd.GetControl("t_list").value = _clocks[clockId].ListName();

}

function OnDestroyOptions(wnd, code) {
  if (code == gddIdCancel)
    return;
  // save the current clock
  SaveClock(GetClockId(wnd.GetControl("t_list").value));
  SaveClocks();
  // save skin, sound,
  options.item("skinDir") = _wndOptions.GetControl("s_list").value;
  options.item("sound") = _wndOptions.GetControl("snd_list").value;
  LoadSkin();
}

//////////////////////
// Utilities	    //
//////////////////////

// type: -1=none, 0=alarm, 1=timer, 2=stopwatch
function SetUIClock(clock) {
  var type = -1;
  if (clock != -1)
    type = clock.type;
  var wnd = _wndOptions;
  wnd.GetControl("a_command").enabled = false;
  wnd.GetControl("a_name").enabled = false;
  wnd.GetControl("a_time").enabled = false;
  wnd.GetControl("t_duration").enabled = false;
  wnd.GetControl("a_time").value = "";
  wnd.GetControl("t_duration").value = "";
  wnd.GetControl("active").enabled = false;
  if (type == -1)
    return;
  wnd.GetControl("active").enabled = true;
  wnd.GetControl("active").value = clock.active;
  wnd.GetControl("a_name").enabled = true;
  wnd.GetControl("a_name").value = clock.name;
  wnd.GetControl("a_command").enabled = true;
  wnd.GetControl("a_command").value = clock.command;
  if (type == 0) {
    wnd.GetControl("a_time").enabled = true;
    wnd.GetControl("a_time").value = clock.alarm;
  } else if (type == 1) {
    wnd.GetControl("t_duration").enabled = true;
    wnd.GetControl("t_duration").value = clock.duration;
  } else if (type == 2) {
    wnd.GetControl("a_command").enabled = false;
  }
}

function SaveClock(clockId) {
  if (clockId == -1)
    return;
  // save the current clock
  var wnd = _wndOptions;
  _clocks[clockId].active = wnd.GetControl("active").value;
  _clocks[clockId].SetName(wnd.GetControl("a_name").value);
  _clocks[clockId].command = wnd.GetControl("a_command").value;
  _clocks[clockId].alarm = wnd.GetControl("a_time").value;
  if (_clocks[clockId].duration != wnd.GetControl("t_duration").value) {
    _clocks[clockId].time = 0;
  }
  _clocks[clockId].duration = wnd.GetControl("t_duration").value;
}

function AddClock(type) {
  Trace("OnAdd");
  var wnd = _wndOptions;
  if (type == 0)
    var c = NewAlarm();
  else if (type == 1)
    var c = NewTimer();
  else if (type == 2)
    var c = NewStopwatch();
  // update the clock list
  _lastSelectedClock = GetClockId(c);
  wnd.GetControl("t_list").text = GetClockList();
  SetUIClock(c);
  wnd.GetControl("t_list").value = c.ListName();
  // refresh the main view
  _currentClock = _clocks.length;
  DrawDetails();
}
